function getBackendURL()
{
   //TODO: If this is null, it should trigger a "Improper configuration" message.
   return localStorage[ "backendURI" ];
}

/**
 * Returns an initialized XmlHTTPRequest based off of the URL provided.
 * @param url	A String representing the full URL + the get variables
 * @returns XmlHTTPRequest Which
 */
function getRequest( url )
{
   var xhr = new XMLHttpRequest();
   xhr.open("GET", url, true);
   return xhr;
}

/**
 * Retrieves the URL for reloading an already saved position
 * @param href  The base URL of the document being reloaded
 * @returns	The complete URL for communication with the 
 * 		back end.
 */
function getFullFetchURL( href )
{
   return getComURL( href, "select" );
}

/**
 * Retrieves the URL for saving a document's position
 * @param href  The base URL of the document being saved
 * @returns	The complete URL for communication with the 
 * 		back end.
 */
function getFullSaveURL( href )
{
   return getComURL( href, "update" );
}

/**
 * A generic means of retrieving a complete URL with GET variables
 * already appended.
 * @param href	The URL which is needed for server communication.
 * @param type	Either "update" or "select" depending on whether this
 * 		will be for either saving (update) or retrieving 
 * 		(select) data.
 * @returns	The complete URL.
 */
function getComURL( href, type )
{
   var pObj    = getUserData();
   var url     = getBackendURL() + "?";
   pObj.action = type;
   // This needs to be here for cases of excessively large data -- parseParams 
   // was having significant difficulty.
   for( var it in pObj ) url += it + "=" + pObj[ it ] + "&"
   return url + "site=" + getGoogleDocsURL( href );
}


/**
 * Returns the value of the URL of the document loaded given the url of the current window/frame
 * @param href     The original URL
 * @returns        The portion related to the current document.
 */
function getGoogleDocsURL( href )
{
   var tmp = "";
   if( href.indexOf( "books.google.com" ) >= 0 || href.indexOf( "docs.google.com" ) >= 0 && href.indexOf( "id=" ) > 0 )
   {
      // Google books has an ID get variable.  Since some of their URLs are close to 200  
      // characters (which is a bit long for the plugin to handle) and that is a unique ID anyway, 
      // we might as well use that.
      tmp = "id";
   }
   else if( href.indexOf( "docs.google.com" ) >= 0 )
   {
      // If Google docs is loading a URL without an ID, it uses the url variable
      tmp = "url";
   }
   return getGetVariable( href, tmp );
}

function getGetVariable( sourceStr, varName )
{
   if( sourceStr.indexOf( varName ) >= 0 && varName != "" )
   {
      var tmp = sourceStr.substr( sourceStr.indexOf( varName ) + varName.length + 1 );
      // Two lines are used because a regex caused crashes.
      sourceStr = tmp.substr( 0, tmp.indexOf( "&" ) );
   }
   return sourceStr;
}
